package com.wzgl.core.webmagic.processor;

import java.io.Serializable;
import java.util.Date;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import com.jfinal.kit.JMap;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.druid.DruidPlugin;
import com.wzgl.core.utils.UuidUtil;
import com.wzgl.core.webmagic.model.WxTgrid;
import com.wzgl.core.webmagic.pipeline.TgridsPipeLine;
import com.wzgl.core.webmagic.utils.DruidPluginUtils;
import com.wzgl.core.webmagic.utils.RedisUtil;
import com.wzgl.core.webmagic.utils.TemplateUtil;

/**
 * @author code4crafter@gmail.com <br>
 * @since 0.6.0 
 */
public class WeiXinPageProcessor implements PageProcessor,Serializable,Job {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/***
	 * addHeader 网站默认是zip模式 如果网站没有使用zip 则需要增加Header
	 */
    private Site site = Site.me().setRetryTimes(1).setSleepTime(1000).addHeader("Accept-Encoding", "/");
    static Spider s ;
    StringBuffer sb = new StringBuffer();
    
    private String activity_name;//标题
	private String post_date;//时间
	private String post_user;//公众号
	private String js_content;//内容
	private String js_pc_qr_code_img;//公众号二维码
	private String css;//样式
	RedisUtil ru = RedisUtil.getRu();
	WxTgrid wx = new WxTgrid();
    @Override
    public void process(Page page) {
    	Document docList = Jsoup.parse(page.getHtml().xpath("//div[@class='rich_media_inner']").get());
    	activity_name = docList.getElementById("activity-name").text();
    	post_date = docList.getElementById("post-date").text();
    	post_user = docList.getElementById("post-user").text();
    	js_content =page.getHtml().xpath("//div[@class='rich_media_content']").toString();
    	css= page.getHtml().xpath("//style").get();
    	String content = js_content.replace("data-src", "src");
    	
    	Object obj = ru.getObj(post_user);
    	if(obj != null){
    		Record r = (Record)obj;
    		wx.setFtype(r.getStr("ftype"));
    		wx.setFreleaseid(r.getStr("fid"));
    		wx.setFlogo(r.getStr("flogo"));
    		wx.setFqrcode(r.getStr("fqrcode"));
    	}
    	wx.setFdate(new Date());
    	wx.setFid(UuidUtil.get32UUID());
    	wx.setFactivity_name(activity_name);
    	wx.setFpost_date(post_date);
    	wx.setFpost_user(post_user);
    	wx.setFjs_content(content);
    	wx.setFcss(css);
    	wx.setFurl(page.getUrl().get());
    	
    	if(!DruidPluginUtils.isStart()){
    		DruidPluginUtils du = new DruidPluginUtils();
    	}
    	boolean b = wx.save();
    	if(b){
    		TemplateUtil.getTemplateWx(wx);
    	}
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
    	 /**
    	  * 单独使用 redis缓存机制
    	  */
//    	 RedisPlugin redisPlugin=new RedisPlugin(CacheConsts.CACHE_MAGIC_GIRD_NAME,"120.77.42.111");
//	     JedisPoolConfig jc = redisPlugin.getJedisPoolConfig();
//	     jc.setTestWhileIdle(true);
//	     jc.setTestOnReturn(true);
//	     jc.setTestOnBorrow(true);
//	     redisPlugin.start();
	     
    	
        s = Spider.create(new WeiXinPageProcessor()).addUrl("http://mp.weixin.qq.com/s/v84evscUetn7dpobGcrNwg").thread(1);    
    	s.addPipeline(new TgridsPipeLine());
    	s.run();
    }

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		// TODO Auto-generated method stub
		s = Spider.create(new WeiXinPageProcessor()).addUrl("http://mp.weixin.qq.com/s/v84evscUetn7dpobGcrNwg").thread(1);    
    	s.addPipeline(new TgridsPipeLine());
    	s.run();
	}
}